package net.xdclass.mq;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.model.ProductRecordMessage;
import net.xdclass.service.ProductTaskService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Classname ProductMqListener
 * @Description TODO
 * @Date 2022/5/24 14:33
 * @Created by sunhao
 */
@Slf4j
@Component
@RabbitListener(queues = "${mqconfig.product_release_queue}")
public class ProductMqListener {

  @Autowired
  private ProductTaskService productTaskService;

  @RabbitHandler
  public void releaseProductRecord(ProductRecordMessage productRecordMessage, Message message, Channel channel) throws IOException {
    long deliveryTag = message.getMessageProperties().getDeliveryTag();
    boolean flag = productTaskService.releaseProductRecord(productRecordMessage);
    if(flag){
      log.info("消息消费成功{}",productRecordMessage);
      channel.basicAck(deliveryTag,false);
    }else{
      log.info("消息消费异常 重新投递{}",productRecordMessage);
      channel.basicReject(deliveryTag,true);
    }
  }
}
